/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 电子商务云服务平台
 * 创建日期 : 2016年6月28日
 * 修改历史 : 
 *     1. [2016年7月15日]修改文件 by lijialun
 * 添加Javadoc注释，更新代码规范，静态工具类增加private构造函数
 */
package com.gavinwind.inspay.common.web.resolver;

import org.apache.commons.fileupload.FileItem;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.ServletContext;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 【类或接口功能描述】
 * 
 * @Description: TODO
 * @author Chenxin
 * @date 2015-7-31 15:31:36
 * @version V1.0
 */
public class MultiCommonsMultipartResolver extends CommonsMultipartResolver {
    /**
     * 
     */
    public MultiCommonsMultipartResolver() {}

    
    /**
     * @param cServletContext
     */
    public MultiCommonsMultipartResolver(ServletContext cServletContext) {
        super(cServletContext);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected MultipartParsingResult parseFileItems(List cFileItems, String cEncoding) {
        Map tMultipartFiles = new HashMap();
        Map tMultipartParameters = new HashMap();

        // Extract multipart files and multipart parameters.
        for (Iterator tIt = cFileItems.iterator(); tIt.hasNext();) {
            FileItem tFileItem = (FileItem) tIt.next();
            if (tFileItem.isFormField()) {
                String tValue = null;
                if (cEncoding != null) {
                    try {
                        tValue = tFileItem.getString(cEncoding);
                    } catch (UnsupportedEncodingException ex) {
                        if (logger.isWarnEnabled()) {
                            logger.warn("Could not decode multipart item '" + tFileItem.getFieldName() + "' with encoding '" + cEncoding
                                    + "': using platform default");
                        }
                        tValue = tFileItem.getString();
                    }
                } else {
                    tValue = tFileItem.getString();
                }
                String[] tCurParam = (String[]) tMultipartParameters.get(tFileItem.getFieldName());
                if (tCurParam == null) {
                    // simple form field
                    tMultipartParameters.put(tFileItem.getFieldName(), new String[] { tValue });
                } else {
                    // array of simple form fields
                    String[] tNewParam = StringUtils.addStringToArray(tCurParam, tValue);
                    tMultipartParameters.put(tFileItem.getFieldName(), tNewParam);
                }
            } else {
                // multipart file field
                CommonsMultipartFile tFile = new CommonsMultipartFile(tFileItem);
                if (tMultipartFiles.put(tFileItem.getName(), tFile) != null) {
                    throw new MultipartException("Multiple files for field name [" + tFile.getName() + "] found - not supported by MultipartResolver");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Found multipart file [" + tFile.getName() + "] of size " + tFile.getSize() + " bytes with original filename ["
                            + tFile.getOriginalFilename() + "], stored " + tFile.getStorageDescription());
                }
            }
        }
        return new MultipartParsingResult(null, tMultipartFiles, tMultipartParameters);
    }
}
